package org.example.generics;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;

public class Demo5<D> {
    D newD;
    public Demo5(){
        try {
            // 通过反射获取model的真实类型
            Type genericSuperclass = this.getClass().getGenericSuperclass();
            System.out.println(genericSuperclass);
            ParameterizedType pt = (ParameterizedType) genericSuperclass;
            System.out.println(pt);
            Type actualTypeArgument = pt.getActualTypeArguments()[0];
            System.out.println(actualTypeArgument);
            Class<D> clazz = (Class<D>) actualTypeArgument;
            System.out.println(clazz);

            // 通过反射创建model的实例
            char[] s = {'1', '3', '4'};
            newD = clazz.getDeclaredConstructor(char[].class).newInstance(s);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    public static void main(String[] args) {
        Demo5<String> dd = new Demo5<>();
        System.out.println(dd.newD);
    }
}
